package com.whut.gkd.service.impl;

import com.whut.gkd.bean.Good;
import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.dao.IGoodDao;
import com.whut.gkd.service.IGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodServiceImpl implements IGoodService {


    @Autowired
    private IGoodDao goodDao;


    @Override
    public PageInfo<Good> findAll(int currentPage, String goodName, int shopId) {
        PageInfo pi = new PageInfo();
        pi.setSize(5);
        int totalcount = goodDao.getTotalCount(goodName, shopId);
        double d = totalcount/5.0;
        int tp = (int) Math.ceil(d);
        pi.setTotalPage(tp);
        if(currentPage<1) currentPage = 1;
        if(currentPage>tp) currentPage = tp;
        pi.setCurrentPage(currentPage);
        int start = (currentPage-1)*5;
        if(totalcount != 0){
            List<Good> goodList = goodDao.findAll(start, 5, goodName, shopId);
            pi.setList(goodList);
        }else{
            pi.setList(null);
        }
        return pi;
    }



    @Override
    public Good selectGoodById(int goodId, int shopId) {
        return goodDao.selectGoodById(goodId, shopId);
    }

    @Override
    public void update(Good good, int shopId) {
        int goodId = good.getGoodId();
        String goodName = good.getGoodName();
        int goodNum = good.getGoodNum();
        double goodPrice = good.getGoodPrice();
        String goodDescription = good.getGoodDescription();
        goodDao.update(goodId, goodName, goodNum, goodPrice, goodDescription, shopId);
    }

    @Override
    public void deleteById(int goodId, int shopId) {
        goodDao.deleteById(goodId,shopId);
    }

    @Override
    public void closeShop(ShopInfo shopinfo) {
        goodDao.deleteGoodsByShopId(shopinfo);
    }


}
